package com.zhulin.android.edoctor.fragment;


import com.zhulin.android.edoctor.MainActivity;
import com.zhulin.android.edoctor.R;
import com.zhulin.android.edoctor.jsonobject.INUserLogin;
import com.zhulin.android.edoctor.jsonobject.OUTUserLogin;
import com.zhulin.android.edoctor.net.AsyncHttpPost;
import com.zhulin.android.edoctor.net.DefaultThreadPool;
import com.zhulin.android.edoctor.parsehandler.UserLoginParseHandler;
import com.zhulin.android.edoctor.util.Command;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

public class FragmentLogin extends BaseFragment implements OnClickListener{

	MainActivity mMainActivity;

	private EditText edtUserName,edtPassword;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mMainActivity=(MainActivity) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		//return super.onCreateView(inflater, container, savedInstanceState);
		View view=inflater.inflate(R.layout.activity_user_login, container, false);

		edtUserName=(EditText)view.findViewById(R.id.edtUserName);
		edtPassword=(EditText)view.findViewById(R.id.edtPassword);

		view.findViewById(R.id.btnLogin).setOnClickListener(this);
		view.findViewById(R.id.btnFindPassword).setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btnFindPassword:{
			mMainActivity.addFragmentOfFindPassword();
			break;
		}case R.id.btnLogin:{
			loginUser();
			break;
		}
		}
	}

	private void loginUser(){
		String strUserName=edtUserName.getText().toString();
		String strPassword=edtPassword.getText().toString();

		INUserLogin inUserLogin=new INUserLogin();
		if(!strUserName.equals("") && !strPassword.equals("")){

			inUserLogin.setLoginName(strUserName);
			inUserLogin.setPassword(strPassword);

			AsyncHttpPost asyncHttpPost=new AsyncHttpPost(new UserLoginParseHandler(), 
					Command.SERVER_ADDRESS+Command.ACTION_UserLogin, inUserLogin.getClassInfo()	,requestResultCallback);
			DefaultThreadPool.getInstance().execute(asyncHttpPost);

			listRequest.add(asyncHttpPost);
		}else{
			Toast(getActivity(), "请输入用户和密码", Toast.LENGTH_LONG);
		}
	}

	@Override
	public void onSuccess(Object o) {
		super.onSuccess(o);
		OUTUserLogin outUserLogin=(OUTUserLogin) o;
		if(outUserLogin.getResult()>0)
		{
			Toast(getActivity(), "登录成功"+outUserLogin.getUserName(), Toast.LENGTH_LONG);
			getFragmentManager().popBackStack();
		}
		else{
			Toast(getActivity(), outUserLogin.getUserName(), Toast.LENGTH_LONG);
		}
	}

	@Override
	public void onFail(Exception e) {
		super.onFail(e);
	}
}
